

import unittest
from multiprocessing import Queue
from planifCortoPlazo_FF import FF
from ManagerResource import ResourceManager
from ManagerCPU import CPUManager
from OS import OS
from Program import Program
from CPUInstruction import CPUInstruction
from ForkInstruction import ForkInstruction
import time


class forkTest(unittest.TestCase):

    def setUp(self):
        self.outputQueue = Queue()
        self.scheduler = FF()
        self.rm = ResourceManager()
        self.rm.registerResource("kb", 1, self.scheduler.getReadyQueue(), self.outputQueue)
        self.rm.registerResource("screen", 1, self.scheduler.getReadyQueue(), self.outputQueue)
        self.rm.registerResource("printer", 1, self.scheduler.getReadyQueue(), self.outputQueue)
        self.cpuManager = CPUManager(1, self.rm,self.outputQueue,self.scheduler)
        self.myOS = OS(self.scheduler, self.rm,self.cpuManager, self.outputQueue)
        self.myProg = Program()
        self.myProg2 = Program()
        self.in1=CPUInstruction(2,"cpu prog1")
        self.in2=CPUInstruction(2,"cpu prog2")
        self.fork=ForkInstruction(self.myProg2)
        self.myProg2.addInstruction(self.in2)
        self.myProg.addInstruction(self.in1) 
        self.myProg.addInstruction(self.fork)
        self.myProg.addInstruction(self.in1)
        self.myOS.addProgram(self.myProg)
        
    def testFork(self):
        self.myOS.startUP()
        time.sleep(10)
        self.assertTrue(self.outputQueue.qsize() == 3)
        self.assertEqual(self.outputQueue.get(), ("cpu prog1", 2))
        self.assertEqual(self.outputQueue.get(), ("cpu prog2", 2))
        self.assertEqual(self.outputQueue.get(), ("cpu prog1", 2))
    
                

    

if __name__ == "__main__":
    #import sys;sys.argv = ['', 'Test.testName']
    unittest.main()
    
suite = unittest.TestLoader().loadTestsFromTestCase(forkTest)
unittest.TextTestRunner(verbosity=2).run(suite)
